package cz.muni.fi.pa165.travelagency.services;

import cz.muni.fi.pa165.travelagency.api.entities.IPackageTour;
import cz.muni.fi.pa165.travelagency.api.services.IPackageTourService;
import cz.muni.fi.pa165.travelagency.dto.PackageTourDTO;
import cz.muni.fi.pa165.travelagency.entities.PackageTour;
import cz.muni.fi.pa165.travelagency.interceptors.ShiroSecured;
import cz.muni.fi.pa165.travelagency.persistence.IPackageTourDAO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;

/**
 * Implementation of a services for PackageTour
 *
 * @author Lukas Jungmann
 * @version 1.0
 */
@Stateless(name = "ptourService", mappedName = "ptourService")
@Local(IPackageTourService.class)
@TransactionManagement
@ShiroSecured
public class PackageTourService implements IPackageTourService {

    @EJB
    private IPackageTourDAO ptDAO;
    private Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();

    void setPackageTourDao(IPackageTourDAO pt) {
        this.ptDAO = pt;
    }

    void setMapper(Mapper mapper) {
        this.mapper = mapper;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    @RequiresPermissions("ptour:create")
    public void createPackageTour(IPackageTour pt) {
        PackageTour p = mapper.map(pt, PackageTour.class);
        ptDAO.createPackageTour(p);
        pt.setId(p.getId());
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    @RequiresPermissions("ptour:update")
    public void updatePackageTour(IPackageTour pt) {
        ptDAO.updatePackageTour(mapper.map(pt, PackageTour.class));
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    @RequiresPermissions("ptour:delete")
    public void deletePackageTour(IPackageTour pt) {
        ptDAO.deletePackageTour(mapper.map(pt, PackageTour.class));
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    @RequiresPermissions("ptour:find")
    public IPackageTour getPackageTour(Long id) {
        PackageTour pt = ptDAO.getPackageTour(id);
        return pt == null ? null : mapper.map(pt, PackageTourDTO.class);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @RequiresPermissions("ptour:find")
    public int getAvailablePlaces(IPackageTour pt) {
        return ptDAO.getAvailablePlaces(mapper.map(pt, PackageTour.class));
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @RequiresPermissions("ptour:find")
    public List<IPackageTour> findAll() {
        List<PackageTour> pts = ptDAO.findAll();
        List<IPackageTour> result = new ArrayList<>();
        for (PackageTour pt : pts) {
            result.add(mapper.map(pt, PackageTourDTO.class));
        }
        return result;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @RequiresPermissions("ptour:find")
    public List<IPackageTour> find(String destination, int maxPrice, Date dateFrom, Date dateTo, int numberOfPersons) {
        List<PackageTour> pts = ptDAO.find(destination, maxPrice, dateFrom, dateTo, numberOfPersons);
        List<IPackageTour> result = new ArrayList<>();
        for (PackageTour pt : pts) {
            result.add(mapper.map(pt, PackageTourDTO.class));
        }
        return result;
    }
}
